#include "projectiles.h"

#include "personnage.h"

struct projectile
{
	int x;
	int y;
	direction d;
	int speed;
	sprite skin;
};

struct projectiles
{
	projectile p;
	projectiles reste;
};

// Fonctions sur un projectile

projectile PROnew()
{
	projectile proj = malloc(sizeof(struct projectile));
	proj->d = DIRnew();
	proj->skin = SPRnew(SPRITE8);
	return proj;
}

void PROfree(projectile proj)
{
	if( proj != NULL )
	{
		SPRfree(proj->skin);
		DIRfree(proj->d);
		free(proj);
	}
}

int PROgetX(projectile proj) { return proj->x; }
int PROgetY(projectile proj) { return proj->y; }
int PROgetSpeed(projectile proj) { return proj->speed; }
sprite PROgetSpr(projectile proj) { return proj->skin; }
direction PROgetDir(projectile proj) { return proj->d; }

void PROsetX(projectile proj, int x) { proj->x = x; }
void PROsetY(projectile proj, int y) { proj->y = y; }
void PROsetSpeed(projectile proj, int spd) { proj->speed = spd; }

void PROdisplay(projectile proj)
{
  SPRblit(proj->skin, proj->x, proj->y);
}

void PROmove(projectile proj, screen e)
{
  PROdisplay(proj);
  proj->x = BORNER(proj->x + DIRgetX(proj->d) * proj->speed, SCRgetXMin(e), SCRgetXMax(e) - 8);
  proj->y = BORNER(proj->y + DIRgetY(proj->d) * proj->speed, SCRgetYMin(e), SCRgetYMax(e) - 8);
  PROdisplay(proj);
}

// Fonctions sur une liste de projectiles

projectiles PROJSnew()
{
	projectiles projs = malloc(sizeof(struct projectiles));
	return projs;
}

void PROJSfree(projectiles projs)
{
	if( projs != NULL )
		free(projs);
}

projectile PROcopy(projectile proj)
{
	projectile p = PROnew();
	p->x = proj->x;
	p->y = proj->y;
	p->speed = proj->speed;
	p->d = DIRcopy(proj->d);
	p->skin = SPRcopy(proj->skin);
	return p;
}

void PROJSpush(projectiles projs, projectile proj)
{
	if( projs != NULL )
	{
		while( projs->reste != NULL)
		{
			projs = projs->reste;
		}
		projectiles newprojs = PROJSnew();
		newprojs->p = PROcopy(proj);
		newprojs->reste = NULL;
		projs->reste = newprojs;
	}
	else
	{
		projs = PROJSnew();
		projs->p = PROcopy(proj);
		projs->reste = NULL;
	}
}

void PROJSmove(projectiles projs, screen e)
{
	if( projs != NULL )
	{
		PROmove(projs->p, e);
		PROJSmove(projs->reste, e);
	}
}
